#include "tool.hpp"
#include "message.hpp"
#include <jsoncpp/json/json.h>

int main()
{
    // INFO_LOG("%s, %d", "hello, world", 613);
    // DEBUG_LOG("%s, %d", "hello, world", 613);
    // ERROR_LOG("%s, %d", "hello, world", 613);
    // RPC::UUID uid;
    // int n = 5;
    // while (n--)
    // {
    //     uid.uuid();
    // }

    // // 测试message类
    // // 1、序列化
    // RPC::BaseMessage::Ptr msg = RPC::MessageFactory::create(RPC::MType::REQ_RPC);
    // RPC::RpcRequest::Ptr rrp = std::dynamic_pointer_cast<RPC::RpcRequest>(msg);
    // rrp->setMethod("add");
    // Json::Value val;
    // val["first"] = 1;
    // val["second"] = 2;
    // rrp->setParam(val);
    // std::string ret = rrp->serialize();
    // std::cout << ret << std::endl;

    // // 2、反序列化
    // RPC::RpcRequest::Ptr rrp2 = RPC::MessageFactory::create<RPC::RpcRequest>();
    // bool retval = rrp2->desrialize(ret);
    // if(retval == false) return -1;
    // std::cout << rrp2->method() << std::endl;
    // Json::Value value = rrp2->Param();
    // int num1 = value["first"].asInt();
    // int num2 = value["second"].asInt();
    // std::cout << "first: " << num1 << " " << "second: " << num2 << std::endl;

    //   测试TopicRequest类
    // 1、序列化
    // RPC::BaseMessage::Ptr msg = RPC::MessageFactory::create(RPC::MType::REQ_TOPIC);
    // RPC::TopicRequest::Ptr rrp = std::dynamic_pointer_cast<RPC::TopicRequest>(msg);
    // rrp->setTopicKey("news");
    // rrp->setMessage("hello, world");
    // rrp->setOpType(RPC::TopicOpType::TOPIC_CREAT);
    // std::string ret = rrp->serialize();
    // std::cout << ret << std::endl;

    // // 2、反序列化
    // RPC::TopicRequest::Ptr rrp2 = RPC::MessageFactory::create<RPC::TopicRequest>();
    // bool retval = rrp2->desrialize(ret);
    // if(retval == false) return -1;
    // std::cout << rrp2->topicKey() << std::endl;
    // std::cout << rrp2->Message() << std::endl;
    // std::cout << (int)rrp2->opType() << std::endl;

    // //   测试ServiceRequest类
    // // 1、序列化
    // RPC::BaseMessage::Ptr msg = RPC::MessageFactory::create(RPC::MType::REQ_SERVICE);
    // RPC::ServiceRequest::Ptr rrp = std::dynamic_pointer_cast<RPC::ServiceRequest>(msg);
    // rrp->setMethod("add");
    // rrp->setOpType(RPC::ServiceOpType::SERVICE_REGISTER);
    // RPC::Address addr;
    // addr.first = "0.0.0.0";
    // addr.second = 8080;
    // rrp->setHost(addr);
    // std::string ret = rrp->serialize();
    // std::cout << ret << std::endl;

    // // 2、反序列化
    // RPC::ServiceRequest::Ptr rrp2 = RPC::MessageFactory::create<RPC::ServiceRequest>();
    // bool retval = rrp2->desrialize(ret);
    // if(retval == false) return -1;
    // std::cout << (int)rrp2->opType() << std::endl;
    // std::cout << rrp2->host().first << " " << rrp2->host().second << std::endl;
    // std::cout << rrp2->method() << std::endl;

    //   测试RpcResponse类
    // 1、序列化
    // RPC::BaseMessage::Ptr msg = RPC::MessageFactory::create(RPC::MType::RES_RPC);
    // RPC::RpcResponse::Ptr rrp = std::dynamic_pointer_cast<RPC::RpcResponse>(msg);
    // Json::Value Jval;
    // Jval["sum"] = 3;
    // rrp->setResult(Jval);
    // rrp->setRCode(RPC::RCode::RCODE_OK);
    // std::string ret = rrp->serialize();
    // std::cout << ret << std::endl;

    // // 2、反序列化
    // RPC::RpcResponse::Ptr rrp2 = RPC::MessageFactory::create<RPC::RpcResponse>();
    // bool retval = rrp2->desrialize(ret);
    // if (retval == false)
    //     return -1;
    // std::cout << (int)rrp2->rCode() << std::endl;
    // Json::Value val = rrp2->Result();
    // std::cout << val["sum"] << std::endl;


    // //   测试TopicResponse类
    // // 1、序列化
    // RPC::BaseMessage::Ptr msg = RPC::MessageFactory::create(RPC::MType::RES_TOPIC);
    // RPC::TopicResponse::Ptr rrp = std::dynamic_pointer_cast<RPC::TopicResponse>(msg);
    
    // rrp->setRCode(RPC::RCode::RCODE_OK);
    // std::string ret = rrp->serialize();
    // std::cout << ret << std::endl;

    // // 2、反序列化
    // RPC::TopicResponse::Ptr rrp2 = RPC::MessageFactory::create<RPC::TopicResponse>();
    // bool retval = rrp2->desrialize(ret);
    // if (retval == false)
    //     return -1;
    // std::cout << (int)rrp2->rCode() << std::endl;

        //   测试ServiceResponse类
    // 1、序列化
    RPC::BaseMessage::Ptr msg = RPC::MessageFactory::create(RPC::MType::RES_SERVICE);
    RPC::ServiceResponse::Ptr rrp = std::dynamic_pointer_cast<RPC::ServiceResponse>(msg);
    rrp->setOpType(RPC::ServiceOpType::SERVICE_DISCOVERY);
    rrp->setRCode(RPC::RCode::RCODE_OK);
    std::string ret = rrp->serialize();
    std::cout << ret << std::endl;

    // 2、反序列化
    RPC::ServiceResponse::Ptr rrp2 = RPC::MessageFactory::create<RPC::ServiceResponse>();
    bool retval = rrp2->desrialize(ret);
    if (retval == false)
        return -1;
    std::cout << (int)rrp2->rCode() << std::endl;
    std::cout << (int)rrp2->Optype() << std::endl;
    return 0;
}